const key = '093e7d32b8bf341331d986a3d180236e'
import {
	getWeatherInfo
} from '@/api/common.js'

// 获取定位
function getLocation(options={}) {
	const {
		type = 'wgs84', geocode = true, isHighAccuracy = true,
	} = options;
	return new Promise(function(resolve, reject) {
		uni.getLocation({
			type,
			geocode,
			isHighAccuracy,
			success: (res) => {
				uni.setStorageSync("location", {
					'latitude': res.latitude,
					'longitude': res.longitude
				})
				resolve(res);
			},
			fail(res) {
				console.log(res)
			},
			complete(res) {
				console.log(res)
			}
		});
	})
}


// 获取天气
async function getWeather(latitude, longitude) {
	const res = await getWeatherInfo({
		lon: longitude,
		lat: latitude,
	});
	let url = ''
	switch (res.data.condition.condition) {
		case '晴':
		case '少云':
		case '晴间多云':
			url = '/static/img/weather/taiyang.png'
			break;
		case '阴':
		case '多云':
			url = '/static/img/weather/yintian.png'
			break;
		case '有风':
		case '平静':
		case '微风':
		case '和风':
		case '清风':
			url = '/static/img/weather/youfeng.png'
			break;
		case '平静':
		case '微风':
		case '和风':
		case '清风':
			url = '/static/img/weather/xiaofeng.png'
			break;
		case '大风':
		case '强风/劲风':
		case '疾风':
		case '烈风':
			url = '/static/img/weather/dafeng.png'
			break;
		case '飓风':
		case '风暴':
		case '狂爆风':
		case '热带风暴':
		case '烈风':
			url = '/static/img/weather/jufeng.png'
			break;

		case '雾':
		case '轻雾':
		case '霾':
			url = '/static/img/weather/youwu.png'
			break;
		case '大雾':
		case '浓雾':
		case '中度霾':
			url = '/static/img/weather/dawutian.png'
			break;
		case '强浓雾':
		case '特强浓雾':
		case '重度霾':
		case '严重霾':
			url = '/static/img/weather/dawutian2.png'
			break;

		case '雾':
		case '轻雾':
		case '霾':
			url = '/static/img/weather/youwu.png'
			break;


		case '阵雨':
			url = '/static/img/weather/xiayu2.png'
			break;

		case '小雨':
		case '毛毛雨/细雨':
		case '雨':
		case '小雨-中雨':
			url = '/static/img/weather/xiaoyu.png'
			break;
		case '雷阵雨':
			url = '/static/img/weather/leizhenyu.png'
			break;
		case '雷阵雨并伴有冰雹':
			url = '/static/img/weather/leizhenyu2.png'
			break;
		case '中雨':
		case '大雨':
		case '暴雨':
		case '大暴雨':
		case '特大暴雨':
		case '强阵雨':
		case '强雷阵雨':
		case '极端降雨':
		case '中雨-大雨':
		case '大雨-暴雨':
		case '暴雨-大暴雨':
		case '大暴雨-特大暴雨':
			url = '/static/img/weather/xiayu.png'
			break;

		case '雪':
		case '阵雪':
		case '小雪':
		case '小雪-中雪':
			url = '/static/img/weather/xiaxue.png'
			break;

		case '雨雪天气':
		case '雨夹雪':
		case '阵雨夹雪':
		case '冻雨':
			url = '/static/img/weather/leizhenyu2.png'
			break;

		case '中雪':
		case '中雪-大雪':
			url = '/static/img/weather/zhongxue.png'
			break;
		case '大雪':
		case '暴雪':
		case '大雪-暴雪':
			url = '/static/img/weather/daxue.png'
			break;


		case '浮尘':
		case '扬沙':
			url = '/static/img/weather/dafeng.png'
			break;

		case '沙尘暴':
		case '强沙尘暴':
			url = '/static/img/weather/jufeng.png'
			break;
		case '龙卷风':
			url = '/static/img/weather/longjuanfeng.png'
			break;

		case '热':
			url = '/static/img/weather/taiyang.png'
			break;
		case '冷':
			url = '/static/img/weather/daxue.png'
			break;
		case '未知':
			url = '/static/img/weather/taiyang.png'
			break;

		default:
			url = '/static/img/weather/taiyang.png'
	}
	let alert = []
	let data = {
		url: url,
		name: res.data.condition.condition,
		earlyWarning: res.data.alert
	}
	uni.setStorageSync('weather', res.data);
	const {
		condition
	} = res.data;
	const weatherDisplay =
		`${condition.condition} ${condition.temp}℃ 日出: ${condition.sunRise.slice(11,16)} 日落: ${condition.sunSet.slice(11,16)}`
	uni.setStorageSync('weatherDisplay', weatherDisplay)
	return data;
}


module.exports = {
	getLocation,
	getWeather
}
